from django.shortcuts import render
from .models import PListModel,UserModel
from .serial import PListCreateSerializer,PListSerializer
from rest_framework.views import APIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework import status

# Create your views here.


class PListCreateView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self,request):
        user = self.request.user
        print(user)
        items = PListModel.objects.filter(user=user)
        print(items)
        data = self.request.data
        print(data)
        serializer = PListCreateSerializer(data=data)
        data['user'] = user.id
        if serializer.is_valid():
            if items.count() > 0:
                for item in items:
                    item.select = "0"
                    item.save()
            serializer.save()
            return Response({'msg': '创建成功'},status=status.HTTP_201_CREATED)
        else:
            return Response({'msg': '创建失败'},status=status.HTTP_400_BAD_REQUEST)

    def put(self,request):
        user = self.request.user
        id = self.request.data["id"]
        redata = self.request.data
        items = PListModel.objects.filter(user=user)
        data = items.get(id=id)
        serializer = PListCreateSerializer(data,data=redata)
        if serializer.is_valid():
            serializer.save()
            for item in items:
                if item.id != id:
                    item.select = "0"
                    item.save()
                else:
                    item.select = "1"
                    item.save()
            return Response({'msg': '修改成功'},status=status.HTTP_200_OK)
        else:
            return Response({'msg': '修改失败'},status=status.HTTP_400_BAD_REQUEST)

    def delete(self,request):
        user = self.request.user
        print(user)
        id = self.request.data.get("id")
        print(id)
        items = PListModel.objects.filter(user=user)
        data = items.get(id=id)
        data.delete()
        return Response({'msg': '删除成功'},status=status.HTTP_200_OK)



class PListView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self,request):
        user = self.request.user
        items = PListModel.objects.filter(user=user)
        if items.count() == 0:
            return Response({'msg': '没有数据'},status=status.HTTP_200_OK)
        else:
            serializer = PListSerializer(items,many=True)
            return Response({'msg': serializer.data},status=status.HTTP_200_OK)






class ReSelectView(APIView):
    permission_classes = [IsAuthenticated]

    def post(self,request):
        user = self.request.user
        id = self.request.data["id"]
        print(id)
        items = PListModel.objects.filter(user=user)
        for item in items:
            if item.id == id:
                if item.select == "1":
                    item.select = "0"
                    item.save()
                else:
                    item.select = "1"
                    item.save()
            else:
                item.select = "0"
                item.save()
        return Response({'msg': '修改成功'},status=status.HTTP_200_OK)



class GetRePdataView(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request, *args, **kwargs):
        user = self.request.user
        id = self.kwargs.get('id')
        if id is not None:
            items = PListModel.objects.filter(user=user)
            data = items.get(id=id)
            serializer = PListCreateSerializer(data)
            return Response({'msg': serializer.data}, status=status.HTTP_200_OK)
        else:
            return Response({'msg': '没有数据'}, status=status.HTTP_200_OK)












